package com.zhoujiulong.coroutineproject

import android.view.MenuItem
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.zhoujiulong.baselib.base.SimpleActivity
import com.zhoujiulong.coroutineproject.databinding.ActivityTestNavigationViewBinding
import com.zhoujiulong.coroutineproject.fragment.Fg01
import com.zhoujiulong.coroutineproject.fragment.Fg02

class TestNavigationView : SimpleActivity<ActivityTestNavigationViewBinding>() {

    private var mFragments = arrayOfNulls<Fragment>(5)
    private var mPreFragmentFlag = 0


    override fun getViewBinding(): ActivityTestNavigationViewBinding {
        return ActivityTestNavigationViewBinding.inflate(layoutInflater)
    }

    override fun initView() {
        initFragment()
        selectFragment()
    }

    override fun initData() {
    }

    override fun getData() {
    }

    fun initFragment() {
        mFragments[0] = Fg01()
        mFragments[1] = Fg02()
        mFragments[2] = Fg01()
        mFragments[3] = Fg02()
        mFragments[4] = Fg01()
        initLoadFragment(R.id.mContainerView, 0, mFragments)
    }

    // 参数一 是一个FrameLayout的ID，用来动态加载Fragment，
    fun initLoadFragment(
        containerId: Int,
        showFragment: Int,
        fragments: Array<Fragment?>
    ) {
        //获取到FragmentManager实例的同时去开启事物
        val transaction =
            supportFragmentManager.beginTransaction()
        for (i in 0 until fragments.size) {
            //首先将Fragment添加到事务中
            transaction?.add(containerId, fragments[i]!!, fragments[i]!!.tag)
            //默认展示 fragments[showFragment]
            //这里做首次Fragment的展示，如果不是指定的Fragment就先隐藏，需要的时候再显示出来
            if (i != showFragment) transaction.hide(fragments[i]!!)
        }
        //提交事物
        transaction.commitAllowingStateLoss()
    }

    open fun selectFragment() {
        //注册监听事件
        mBinding.mBottomNav.setItemIconTintList(null)
        mBinding.mBottomNav.setOnNavigationItemSelectedListener(
            BottomNavigationView.OnNavigationItemSelectedListener { menuItem: MenuItem ->
                when (menuItem.itemId) {
                    R.id.home -> {
                        showAndHideFragment(
                            mFragments[0]!!,
                            mFragments[mPreFragmentFlag]!!
                        )
                        mPreFragmentFlag = 0
                    }
                    R.id.wechat -> {
                        showAndHideFragment(
                            mFragments[1]!!,
                            mFragments[mPreFragmentFlag]!!
                        )
                        mPreFragmentFlag = 1
                    }
                    R.id.project -> {
                        showAndHideFragment(
                            mFragments[2]!!,
                            mFragments[mPreFragmentFlag]!!
                        )
                        mPreFragmentFlag = 2
                    }
                    R.id.system -> {
                        mFragments[mPreFragmentFlag]?.let {
                            showAndHideFragment(
                                mFragments[3]!!,
                                it
                            )
                        }
                        mPreFragmentFlag = 3
                    }
                    R.id.setting -> {
                        mFragments[4]?.let {
                            mFragments[mPreFragmentFlag]?.let { it1 ->
                                showAndHideFragment(
                                    it,
                                    it1
                                )
                            }
                        }
                        mPreFragmentFlag = 4
                    }
                }
                true
            }
        )
    }

    //加载不同的Fragment
    open fun showAndHideFragment(
        show: Fragment,
        hide: Fragment
    ) {
        val transaction =
            supportFragmentManager.beginTransaction()
        if (show !== hide) transaction.show(show).hide(hide).commitAllowingStateLoss()
    }
}